<?php
declare (strict_types = 1);

namespace app\common\system\logic;

use app\common\BaseLogic;
use app\common\system\model\AuthNode;

class UserRoleLogic extends BaseLogic {
    // 获取用户的功能项
    public function getUserFuncItems($userId)
    {
        $nodeLogic = new AuthNodeLogic();
        if ($userId == 1) {
            $nodes = $nodeLogic->getAdminNode();
        } else {
            $ids = $this->getPcRules($userId);
            if (empty($ids)) return [];

            $nodes = $nodeLogic->getNodeByIds($ids);
        }

        return empty($nodes) ? [] : array_column($nodes, 'name');
    }

    // 获取用户的菜单项
    public function getMenuItems($userId)
    {
        $ids = $this->getPcRules($userId);
        if(empty($ids)) return [];

        $nodeLogic = new AuthNodeLogic();
        $nodes = $nodeLogic->getMenuByIds($ids);
       
        return $nodes;
    }

    // 获取用户的PC端规则
    public function getPcRules($userId)
    {
        $roleIds = $this->getUserRole($userId);
        if(empty($roleIds)) return false;

        $rules = [];
        $roleModel = new AuthRoleLogic();
        $roles = $roleModel->getRoles($roleIds, false);
        foreach ($roles as $role) {
            $role = $role->visible(['pc']);
            if (!empty($role['pc'])) {
                $rules = array_merge($rules, $role['pc']);
            }
        }
        $rules = array_unique($rules);

        return $rules;
    }

    // 获取用户的所有角色
    public function getUserRole($userId, $isOnlyIds=true)
    {
        $where = [];
        $where[] = ['user_id', '=', $userId];
        $model = new \app\common\system\model\UserRole();
        $rows = $model->where($where)->select();

        if (!$isOnlyIds) {
            if ($rows->isEmpty()) {
                $data = [];
            } else {
                $ids = $rows->column('role_id');
                $roleLogic = new AuthRoleLogic();
                $data = $roleLogic->getRoles($ids);
            }
        } else {
            $data = $rows->column('role_id');
        }

        return $data;
    }

    public function getUserRoleNames($userId) : Array {

        $where = [];
        $where[] = ['user_id', '=', $userId];
        $model = new \app\common\system\model\UserRole();
        $rows = $model->where($where)->select();

        if ($rows->isEmpty()) {
            $data = [];
        } else {
            $ids = $rows->column('role_id');
            $roleLogic = new AuthRoleLogic();
            $rows = $roleLogic->getRoles($ids, false);
            if ($rows->isEmpty()) {
                $data = [];
            } else {
                $data = $rows->column('name');
            }
        }
        
        return $data;
    }

    // 更新用户权限
    public function updateUserRole($userId, $ids, $operatorId)
    {
        // 获取当前已有的
        $where = [];
        $where[] = ['user_id', '=', $userId];
        $model = new \app\common\system\model\UserRole();
        $currentRoles = $model->where($where)->select();
        $currentIds = $currentRoles->column('role_id');
        $needDeleteIds = array_diff($currentIds, $ids);
        foreach ($currentRoles as $row) {
            if (in_array($row->role_id, $needDeleteIds)) {
                $row->delete(true);
            }
        }

        $batchData = [];
        $data = [];
        $data['operator_id'] = $operatorId;
        $data['user_id'] = $userId;
        $data['create_time'] = time();
        $needAppendIds = array_diff($ids, $currentIds);
        foreach ($needAppendIds as $id) {
            $data['role_id'] = $id;
            array_push($batchData, $data);
        }
        $result = $model->insertAll($batchData);

        return $result;
    }
}